package br.com.fiap.trabalho.junit;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.junit.Test;

import br.com.fiap.trabalho.dao.MovieDAO;
import br.com.fiap.trabalho.entity.Actor;
import br.com.fiap.trabalho.entity.Category;
import br.com.fiap.trabalho.entity.Movie;
import br.com.fiap.trabalho.entity.Studio;
import br.com.fiap.trabalho.factory.DaoFactory;

public class JPAMovieDAOTestMetric extends DAOtest{

	private static MovieDAO movieDAO = DaoFactory.getDaoFactory(DaoFactory.JDBC).getMovieDAO();
	
	public void createEntity() {

		for(int i = 0; i < numberOfIteractions; i++){

			Calendar calendar = Calendar.getInstance();
			calendar.set(1989, Calendar.OCTOBER, 04);
			Date birthDate = calendar.getTime();
			String actorName = "Jose dos testes"+i;
			
			Actor actor = new Actor();
			actor.setFullName(actorName);
			actor.setBirthDate(birthDate);

			Category category = new Category();
			category.setName("Categoria de testes"+i);

			Studio studio = new Studio();
			studio.setName("Studio de teste"+i);

			Movie movie = new Movie();
			movie.setTitle("Filme do Joao dos Testes"+i);
			movie.setYear(2013);
			movie.setActors(Arrays.asList(actor));
			movie.setStudio(studio);
			movie.setCategories(Arrays.asList(category));
			movieDAO.createMovie(movie);
		}

		System.out.println("Tempo para criar entidades Movie JPA: ");
	}

	public void listEntity() {
		
		movieDAO.selectMovies(numberOfIteractions);
		
		System.out.println("Tempo para Listar entidades Movie JPA: ");
	}

	public void updateEntity() {

		List<Movie> movieForUpdate = movieDAO.selectMovies(numberOfIteractions);
		
		int i = 0;
		for(Movie movie : movieForUpdate){
			
			Calendar calendar = Calendar.getInstance();
			calendar.set(1989, Calendar.OCTOBER, 04);
			Date birthDate = calendar.getTime();
			String actorName = "Jose dos testes update"+i;
			
			Actor actor = new Actor();
			actor.setFullName(actorName);
			actor.setBirthDate(birthDate);

			Category category = new Category();
			category.setName("Categoria de testes update"+i);

			Studio studio = new Studio();
			studio.setName("Studio de teste update"+i);

			movie.setTitle("Filme do Joao dos Testes update"+i);
			movie.setYear(2013);
			movie.setActors(Arrays.asList(actor));
			movie.setStudio(studio);
			movie.setCategories(Arrays.asList(category));
			
			movieDAO.createMovie(movie);
			i++;
		}
		
		System.out.println("Tempo para Update entidades Movie JPA: ");
	}
	
	@Test
	public void getcategoriesWithAttributes() {
		
		List<Movie> movieWithAttributes = movieDAO.selectMovies(numberOfIteractions);

		for(Movie movie : movieWithAttributes){
			
			movieDAO.selectMovieByYear(movie.getYear());
		}
		
		System.out.println("Tempo para Listar Movie with Attributes JPA: ");
	}
}
